<!-- 游戏水位观察 -->
<template>
  <div class="padding20">
    <!-- 游戏水位曲线 -->
    <div class="border-line" style="margin-bottom: 10px">
      <LargeChart chartTitle="游戏水位曲线" :xAxisData="data.waterXAxis" :seriesData="data.waterSeries" />
    </div>
    <SearchTable :query="data.query" :list="data.list" :total="data.total" @changeSize="changeSize"
      @changePage="changePage" @search="getSearchList" @reset="onHideDetails(true)" @changeTh="data.dialogVisible = true">
      <template #form>
        <Operator ref="OperatorRef" @query="queryItem" />
        <el-form-item label="游戏：">
          <el-select v-model="data.query.game_id" placeholder="请选择" filterable>
            <el-option v-for="item in indexStore.gameArray" :key="item.value"
              :label="'【' + item.value + '】' + item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="日期时间：">
          <el-date-picker v-model="data.query.date" type="daterange" range-separator="-" start-placeholder="请选择开始时间"
            end-placeholder="请选择结束时间" value-format="YYYY-MM-DD" clearable />
        </el-form-item>
      </template>
      <template #button></template>
      <template #table>
        <template v-for="(item, index) in data.fields" :key="index">
          <el-table-column v-if="item.visible" :prop="item.prop" :label="item.label" :width="item.width">
            <template v-if="item.prop == 'save_time_stamp'" #default="scope">
              {{ setTimestampTime(scope.row.save_time_stamp) }}
            </template>
            <template v-if="item.prop == 'agent_id'" #default="scope">
              {{ selectToLabel(indexStore.agentArray, scope.row.agent_id) }}
            </template>
            <template v-if="item.prop == 'account_id'" #default="scope">
              {{ scope.row.account }}
            </template>
            <template v-if="item.prop == 'game_id'" #default="scope">
              <div class="gameIcon">
                <div><img class="game-icon" :src="selectToLabel(GAMEICON, scope.row.game_id)" alt=""></div>
                {{ selectToLabel(indexStore.gameArray, scope.row.game_id) }}
              </div>
            </template>
            <template v-if="item.prop == 'enter_money'" #default="scope">
            <span :class="colorClass(scope.row.enter_money)">{{BigWan(scope.row.enter_money)}}</span>
          </template>
          <template v-if="item.prop == 'after_settlement_money'" #default="scope">
            <span :class="colorClass(scope.row.after_settlement_money)">{{BigWan(scope.row.after_settlement_money)}}</span>
          </template>
          <template v-if="item.prop == 'bet'" #default="scope">
            <span :class="colorClass(scope.row.bet)">{{BigWan(scope.row.bet)}}</span>
          </template>
          <template v-if="item.prop == 'win_lose'" #default="scope">
            <span :class="colorClass(scope.row.win_lose)">{{BigWan(scope.row.win_lose)}}</span>
          </template>
          </el-table-column>
        </template>
        <el-table-column fixed="right" label="操作" width="90" align="center">
        <template #default="scope">
            <el-button :disabled="!scope.row.log_details_id && !scope.row.game_type ? true : false" type="primary"
              v-permiss="'/system/destroyManager'" size="small" @click="checkDetails(scope.row)">
              详情
            </el-button>
          </template>
        </el-table-column>
      </template>
    </SearchTable>
    <!-- 变更表头弹框 -->
    <tableHeader :dialogVisible="data.dialogVisible" :list="data.fields" @cancel="onHideDetails"
      @resetData="resetData"></tableHeader>

    <!-- 游戏详情 -->
    <GameDetail v-if="data.isShowDetails" :show="data.isShowDetails" :checkedItem="data.checkedItem" @onHideDetails="onHideDetails"/>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, toRefs, onMounted, computed } from 'vue'
import Operator, { type QueryType } from '@/components/FormItem/Operator.vue'
import LargeChart from '@/components/Echarts/largeChart.vue'
import tableHeader from '@/components/tableHeader/tableHeader.vue'
import SearchTable from '@/components/SearchTable/SearchTable.vue'
import GameDetail from '@/views/player/components/game-details.vue'
import { GAMEICON } from '@/config/eunm'
import { BigWan, BigMinus } from '@/utils/math'
import { selectToLabel, colorClass } from '@/utils/index'
import { getDateDay, setTimeValue, setTimestampTime } from '@/utils/time'
import { getWaterChart, getWaterTable } from '@/api/game'
import { useStore } from '@/stores/index'
const indexStore = useStore()
export interface commercialType {
  value: number
  label: string
}
const data = reactive({
  query: {
    page: 1,
    size: 50,
    agent_id: indexStore.agentArray[0]?.value,
    operated_id: indexStore.operatorArray[0].value,
    game_id: indexStore.gameArray[0].value,
    currency_id: -1,
    date: [getDateDay(0), getDateDay(0)]
  },
  list: [],
  total: 0,
  fields: [
    { label: '创建时间', prop: 'save_time_stamp', width: '180', visible: true },
    { label: '牌局编号', prop: 'round_id_str', width: '200', visible: true },
    { label: '平台昵称', prop: 'account_id', width: '200', visible: true },
    { label: '商户昵称', prop: 'agent_id', width: '200', visible: true },
    { label: '游戏名称', prop: 'game_id', width: '200', visible: true },
    { label: '下注前', prop: 'enter_money', width: '200', visible: true },
    { label: '结算后', prop: 'after_settlement_money', width: '200', visible: true },
    { label: '下注金额', prop: 'bet', width: '120', visible: true },
    { label: '玩家输赢', prop: 'win_lose', width: '150', visible: true },
  ],
  checkedItem: {} as any,
  dialogVisible: false,
  isShowDetails: false,
  exportLoading: false,
  waterXAxis: [] as string[],
  waterSeries: [] as number[],
  totalDetails: 0
})

// 商户类别 + 商户
const queryItem = (item: QueryType) => {
  data.query.agent_id = item.agent_id
  data.query.operated_id = item.operated_id
}

onMounted(async () => {
  getOperatedList()
})

const getSearchList = async () => {
  data.query = setTimeValue(data.query)
  indexStore.setPageLoading(true)
  data.waterSeries = []
  data.waterXAxis = []
  let [chart,table] = await Promise.all([getWaterChart(data.query), getWaterTable(data.query)])
  // 折线图
  chart.data?.forEach((item: { date: number,water_level: number }) => {
    data.waterSeries.push(BigWan(item.water_level))
    data.waterXAxis.push(setTimestampTime(item.date).substring(5,16))
  })
  // 列表
  table.data.list?.forEach((item:any) => {
    item.win_lose= BigMinus(item.win,item.bet) //玩家输赢 = 玩家赢钱 - 玩家下注
  });
  data.list = table.data.list
  data.total = table.data.total
  indexStore.setPageLoading(false)
}

// 选择总代-查询总代下所有商户
const getOperatedList = async () => {
  const query = OperatorRef.value.getFirstSearchList()
  query.then((res:any)=>{
    data.query.agent_id = res.agent_id
    data.query.operated_id = res.operated_id
    getSearchList()
  })
}

// 详情
const checkDetails = async (row: any) => {
  data.isShowDetails = true
  data.checkedItem = row
}

// 修改页数
const changeSize = (val: number) => {
  data.query.size = val
  getSearchList()
}

const changePage = (val: number) => {
  data.query.page = val
  getSearchList()
}

// 重置表头
const resetData = (val: any) => {
  data.fields = val
}

// 重置
const OperatorRef = ref<any>()
const onHideDetails = (show: boolean) => {
  data.isShowDetails = false
  data.dialogVisible = false
  if (show) {
    data.query = {
      page: 1,
      size: 50,
      agent_id: indexStore.agentArray[0]?.value,
      operated_id: indexStore.operatorArray[0].value,
      game_id: indexStore.gameArray[0].value,
      currency_id: -1,
      date: [getDateDay(0), getDateDay(0)]
    }
    getOperatedList()
  }
}
</script>

<style scoped lang="scss">
.padding20 {
  padding-top: 10px;
  background-color: var(--background) !important;
}
</style>
